package core;

import java.util.Iterator;

import core.traits.Trait;

import util.NameSet;
import util.TraitFactory;

/**
 * Players are collections of traits
 * 
 * @version 1.4
 * @author Alexander Schearer <aas11@duke.edu>
 */
public class Player implements Iterable<Trait>, Named<Player> {

	private String myName;

	private NameSet<Trait> myTraits;

	public Player(String name) {
		myName = name;
		myTraits = new NameSet<Trait>();
	}

	public String getName() {
		return myName;
	}

	public void remove(String trait) {
		myTraits.remove(trait);
	}

	/**
	 * Update a specified trait with a given value
	 * 
	 * This method will garuntee that the specified trait is updated.  If the
	 * trait does not exist then it will be created and added to the player's
	 * list of existing traits.
	 * 
	 * @param trait
	 * @param value
	 */
	public void update(String trait, int value) {
		if (myTraits.contains(trait)) {
			myTraits.get(trait).update(value);
		} else {
			Trait t = TraitFactory.getInstance(trait);
			t.update(value);
			myTraits.add(t);
		}
	}

	/**
	 * Retrieve a list of traits' names which belong to the player
	 */
	public Iterator<Trait> iterator() {
		return myTraits.iterator();
	}

	public boolean equals(Player i) {
		return myName.equals(i.getName());
	}

	public int compareTo(Player o) {
		return myName.compareTo(o.getName());
	}

}
